package edu.ccut.saturn.studio.database.adapters;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.studio.database.model.DBConnection;

public class MySQLAdapter extends BaseDatatbaseAdapter {

	public String filterTableName(String s, String s1) {
		return s1.toUpperCase();
	}

	public ResultSet getColumns(DatabaseMetaData metaData, String catalog,
			String schema, String table, String columnNamePattern)
			throws SQLException {

		return super.getColumns(metaData, schema, catalog, table,
				columnNamePattern);
	}

	@Override
	public ResultSet getPrimerKey(DatabaseMetaData metaData, String catalog,
			String schema, String table) throws SQLException {

		return super.getPrimerKey(metaData, schema, catalog, table);
	}

	public List<String> getSchemas(DatabaseMetaData metaData,
			ResultSet schemaRts, List<String> schemaNames) throws Exception {
		schemaRts = metaData.getCatalogs();
		while (schemaRts.next()) {
			schemaNames.add(schemaRts.getString("TABLE_CAT"));
		}
		return schemaNames;
	}

	public List<String> getTables(DatabaseMetaData metaData, String catalog,
			String schemaPattern, String tableNamePattern, String type,
			ResultSet resultSet) throws SQLException {
		List<String> names = new ArrayList<String>();

		resultSet = metaData.getTables(schemaPattern.toUpperCase(), "", "%",
				new String[] { "VIEW" });
		while (resultSet.next()) {
			if ("VIEW" == type
					&& "VIEW".equalsIgnoreCase(resultSet.getString(4))) {
				names.add(resultSet.getString(3));
			} else if (type == "TABLE"
					&& "TABLE".equalsIgnoreCase(resultSet.getString(4))) {
				names.add(resultSet.getString(3));
			}
		}
		return names;
	}

	@Override
	public String getDatabaseName(DBConnection dbConnection) {
		String[] dbnamedetail = dbConnection.getUrl()
				.replace("://", "").split("/"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		return dbnamedetail[1];
	}

}
